package 回溯;

public class LeetCode130_被围绕的区域 {
    public int[][] nextP = {{1,0},{-1,0},{0,1},{0,-1}};
    public void dfs(char[][] board, int row,int col, int i, int j) {
        // 当前位置设置为'*'
        board[i][j] = '*';
        for(int k = 0; k < 4; ++k) {
            int ni = i + nextP[k][0];
            int nj = j + nextP[k][1];
            // 判断边界
            if(ni < 0 || ni >= row || nj < 0 || nj >= col) {
                continue;
            }
            // 是'O'说明和边联通，继续搜索是否还有联通的
            if(board[ni][nj] != '*' && board[ni][nj] != 'X'){
                dfs(board, row, col, ni, nj);
            }
        }
    }
    public void solve(char[][] board) {
        if(board.length == 0) {
            return;
        }
        //寻找边上的每一个O，如果没有，表示所有的O都被包围
        int row = board.length;
        int col = board[0].length;

        // 寻找第一行和最后一行
        for(int j = 0;j < col; ++j) {
            if(board[0][j] == 'O') {
                dfs(board, row, col, 0, j);
            }
            if(board[row - 1][j] == 'O') {
                dfs(board, row, col, row-1, j);
            }
        }

        // 寻找第一列和最后一列
        for(int j = 0; j < row; ++j) {
            if(board[j][0] == 'O') {
                dfs(board,row,col,j,0);
            }
            if(board[j][col-1] == 'O') {
                dfs(board,row,col,j,col-1);
            }
        }

        for(int i = 0; i < row; ++i) {
            for(int j = 0; j < col; ++j) {
                if(board[i][j] == '*') {
                    board[i][j] = 'O';
                }else if(board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
            }
        }
    }
}
